from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
from sqlalchemy.orm import relationship, backref
from datetime import datetime

Base = declarative_base()

__all__ = ['Config', 'Driver', 'AnalysisProfile', 'Option']

class Config(Base):
    __tablename__ = 'configs'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    analysis_profile_id = Column(Integer, ForeignKey('analysis_profiles.id'))
    driver_id = Column(Integer, ForeignKey('drivers.id'))
    created_at = Column(DateTime, default=datetime.now())
    updated_at = Column(DateTime, default=datetime.now())
    driver = relationship("Driver", backref=backref('configs', order_by=id))
    analysis_profile = relationship("AnalysisProfile", backref=backref('configs', order_by=id))

    def __init__(self, conf_name, analysis_profile_id, driver_id):
        self.name = conf_name
        self.analysis_profile_id = analysis_profile_id
        self.driver_id = driver_id
        self.created_at = datetime.now()
        self.updated_at = datetime.now()

    def __repr__(self):
       return "<Config('%s','%s', '%s', '%s')>" % (self.name, self.analysis_profile_id, self.driver_id, self.created_at)

class Driver(Base):
    """Database Drivers"""
  
    __tablename__ = 'drivers'

    id = Column(Integer, primary_key=True)
    driver = Column(String)
    alias = Column(String)
  
    def __init__(self, driver, database_name):
      self.driver = driver
      self.alias = database_name

    def __repr__(self):
       return "<Driver('%s','%s')>" % (self.driver, self.alias)


class AnalysisProfile(Base):
    """Analysis Profile"""
  
    __tablename__ = 'analysis_profiles'

    id = Column(Integer, primary_key=True)
    host = Column(String)
    data_directory = Column(String)
    html_directory = Column(String)
    results_directory = Column(String)
    created_at = Column(DateTime, default=datetime.now())
    updated_at = Column(DateTime, default=datetime.now())
  
    def __init__(self, host_name, data, html, results):

      self.host = host_name
      self.data_directory = data
      self.html_directory = html
      self.results_directory = results

    def __repr__(self):
       return "<AnalysisEvent('%s','%s','%s','%s')>" % (self.host, self.data_directory, self.html_directory, self.results_directory)

class Option(Base):
  """Provides a way to store project options"""

  __tablename__ = 'options'

  id = Column(Integer, primary_key=True)
  key = Column(String, unique=True)
  value = Column(String)
  created_at = Column(DateTime, default=datetime.now())
  updated_at = Column(DateTime, default=datetime.now())

  def __init__(self, key, value):
    self.key = key
    self.value = value

  def __repr__(self):
    return "<Option('%s', '%s')>" % (self.key, self.value)

